package com.hainatong.jcmvvmbase;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import com.hainatong.jcmvvmbase.api.SiteHelperService;
import com.hainatong.jcmvvmbase.dbtoolroom.Injection;
import com.hainatong.jcmvvmbase.dbtoolroom.User;
import com.hainatong.jcmvvmbase.dbtoolroom.UserDataCallback;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.http.ApiBuildFactory;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;
import com.hainatong.mvvmbase.utils.L;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class FirstViewModel extends BaseViewModel
{
    public final ObservableField<String> text = new ObservableField<>();

    public FirstViewModel(@NonNull Application application)
    {
        super(application);
    }


    public MutableLiveData<String> getInfoByNet(final String name1 )
    {
        MutableLiveData<String> mResult = new MutableLiveData<>();

        Map<String,String> params = new HashMap<>();
        params.put("username","user01");
        params.put("password","123");

        ApiBuildFactory.getInstance()
                       .create(SiteHelperService.class, SiteHelperService.BASE_URL)
                       .login(params)
                       .compose(RxSchedulersUtils.Obs_io_main())
                       .subscribe(new Observer<Object>()
                       {
                           @Override
                           public void onSubscribe(Disposable d)
                           {
                               addDisposable( d );
                           }

                           @Override
                           public void onNext(Object o)
                           {
                               mResult.setValue( name1 + o.toString() );

                           }

                           @Override
                           public void onError(Throwable e)
                           {

                           }

                           @Override
                           public void onComplete()
                           {

                           }
                       });

        return mResult;
    }



    public MutableLiveData<User> saveUser(User user)
    {
        MutableLiveData<User> mResult = new MutableLiveData<>();

        Injection.get().addData(user);

        Injection.get().getSingleBean(new UserDataCallback() {
            @Override
            public void onDataNotAvailable()
            {

            }

            @Override
            public void getData(User bean)
            {
                L.e( "DB",bean.toString() );
                mResult.setValue( bean );
            }
        });

        return mResult;
    }

}
